﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using DotNetNuke.Modules.Help;
using DotNetNuke.Modules.Help.Tests.Mocks;

namespace DotNetNuke.Modules.Help.Tests
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class EditHelpCategoryFixture
    {
        readonly MockEditHelpCategory _view;
        readonly EditHelpCategoryPresenter _editHelpCategoryPresenter;

        public EditHelpCategoryFixture()
        {
            _view = new MockEditHelpCategory();
            var _mockHelpCategoryController = new MockHelpCategoryController();
            _editHelpCategoryPresenter = new EditHelpCategoryPresenter(_view, _mockHelpCategoryController);
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        #region Load Fixture

        [TestMethod]
        [Description("Loads the Edit page ready to add a new item")]
        [Owner("Philip Beadle")]
        public void EditPageLoadNewItem()
        {
            SetView();

            _editHelpCategoryPresenter.OnViewLoad(false);

            Assert.IsFalse(_view.DeleteButtonVisible);
            Assert.IsFalse(_view.AuditControlVisible);
        }

        [TestMethod]
        [Description("Loads the Edit page ready to edit an existing item")]
        [Owner("Philip Beadle")]
        public void EditPageLoadEdit()
        {
            SetView();

            _view.ItemId = MockHelper.ValidHelpInfoId;
            _editHelpCategoryPresenter.OnViewLoad(false);

            Assert.IsTrue(_view.DeleteButtonVisible);
            Assert.IsTrue(_view.AuditControlVisible);
            //Assert.AreEqual<string>(MockHelper.content, _view.EditorContent);
            //Assert.AreEqual<string>(MockHelper.createdByUserName, _view.AuditCreatedByUser);
        }

        [TestMethod]
        [Description("Loads the Edit page which receives a null value from the controller")]
        [Owner("Philip Beadle")]
        public void EditPageLoadNullTestDrivenDNNModuleInfo()
        {
            SetView();
            _view.ItemId = MockHelper.NullHelpInfoId;
            _editHelpCategoryPresenter.OnViewLoad(false);

            Assert.IsTrue(_view.SecurityViolation);
        }

        [TestMethod]
        [Description("Loads the Edit page which causes an exception")]
        [Owner("Philip Beadle")]
        public void EditPageLoadException()
        {
            SetView();
            _view.ItemId = MockHelper.LoadExceptionId;
            _editHelpCategoryPresenter.OnViewLoad(false);

            Assert.AreEqual<string>("Get Exception", _view.Exception.Message);
        }

        #endregion

        #region Cancel Fixture
        [TestMethod]
        [Description("Cancels the Edit page")]
        [Owner("Philip Beadle")]
        public void EditPageCancel()
        {
            _editHelpCategoryPresenter.OnCancel();
            Assert.IsTrue(_view.Redirect);
        }
        #endregion

        #region Delete Fixture
        [TestMethod]
        [Description("Deletes an existing item")]
        [Owner("Philip Beadle")]
        public void EditPageDelete()
        {
            SetView();

            _view.ItemId = MockHelper.ValidHelpInfoId;
            _editHelpCategoryPresenter.OnDelete();

            Assert.IsFalse(_view.SecurityViolation);
            Assert.IsTrue(_view.Redirect);
            Assert.IsNull(_view.Exception);
        }

        [TestMethod]
        [Description("Tries to delete an non existent item which causes a security exception")]
        [Owner("Philip Beadle")]
        public void EditPageDeleteNullTestDrivenDNNModuleInfo()
        {
            SetView();

            _view.ItemId = MockHelper.NullHelpInfoId;
            _editHelpCategoryPresenter.OnDelete();

            Assert.IsTrue(_view.SecurityViolation);
            Assert.IsFalse(_view.Redirect);
            Assert.IsNull(_view.Exception);
        }

        [TestMethod]
        [Description("Causes an exception in the delete method")]
        [Owner("Philip Beadle")]
        public void EditPageDeleteException()
        {
            SetView();
            _view.ItemId = MockHelper.DeleteExceptionId;
            _editHelpCategoryPresenter.OnDelete();

            Assert.AreEqual<string>("Delete Exception", _view.Exception.Message);
        }
        #endregion

        #region Update Fixture

        [TestMethod]
        [Description("Successfully adds new item")]
        [Owner("Philip Beadle")]
        public void EditPageAddValid()
        {
            SetView();
            _view.ModuleId = MockHelper.ValidHelpInfoId;
            _editHelpCategoryPresenter.OnUpdate();

            Assert.IsTrue(_view.Redirect);
            Assert.IsNull(_view.Exception);
        }

        [TestMethod]
        [Description("Tries to add a new item but causes an exception")]
        [Owner("Philip Beadle")]
        public void EditPageAddException()
        {
            SetView();
            //In this case set the ModuleID to the exception value as the ItemId needs to be -1 
            _view.ModuleId = MockHelper.AddExceptionId;
            _editHelpCategoryPresenter.OnUpdate();

            Assert.AreEqual<string>("Add Exception", _view.Exception.Message);
        }

        [TestMethod]
        [Description("Successfully updates an item")]
        [Owner("Philip Beadle")]
        public void EditPageUpdateValid()
        {
            SetView();
            _view.ItemId = MockHelper.ValidHelpInfoId;
            _editHelpCategoryPresenter.OnUpdate();

            Assert.IsTrue(_view.Redirect);
            Assert.IsNull(_view.Exception);
        }

        [TestMethod]
        [Description("Unsuccessfully updates an item")]
        [Owner("Philip Beadle")]
        public void EditPageUpdateNullOnAdd()
        {
            SetView();
            _view.ModuleId = MockHelper.NullHelpInfoId;
            _editHelpCategoryPresenter.OnUpdate();

            Assert.AreEqual<string>("Add Exception", _view.Exception.Message);
        }

        [TestMethod]
        [Description("Tries to update an item but causes an exception")]
        [Owner("Philip Beadle")]
        public void EditPageUpdateException()
        {
            SetView();
            _view.ItemId = MockHelper.UpdateExceptionId;
            _editHelpCategoryPresenter.OnUpdate();

            Assert.AreEqual<string>("Update Exception", _view.Exception.Message);
        }

        #endregion

        private void SetView()
        {
            _view.ItemId = DotNetNuke.Common.Utilities.Null.NullInteger;
            _view.AuditControlVisible = false;
            _view.AuditCreatedByUser = "test";
            _view.DeleteButtonVisible = true;
            _view.AuditControlVisible = true;

        }
    }
}
